package com.admin.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.admin.model.base.PageResult;
import com.admin.model.dto.TypeMappingDto;
import com.admin.model.pojo.TypeMapping;
import com.admin.system.dao.TypeMappingDao;
import com.admin.system.service.TypeMappingService;
import com.admin.system.service.mapstruct.TypeMappingMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * (TypeMapping)表服务实现类
 *
 * @author caixiaofeng
 * @since 2021-03-13 21:49:25
 */
@Service("ganTypeMappingService")
public class TypeMappingServiceImpl extends ServiceImpl<TypeMappingDao, TypeMapping> implements TypeMappingService {
    @Resource
    private TypeMappingDao typeMappingDao;
    @Resource
    private TypeMappingMapper typeMappingMapper;

    @Override
    public TypeMappingDto findById(Long id) {
        return typeMappingMapper.toDto(typeMappingDao.selectById(id));
    }

    @Override
    public PageResult queryAll(TypeMappingDto typeMappingDto, int pageNum, int pageSize, String orderBy) {
        TypeMapping typeMapping = typeMappingMapper.toEntity(typeMappingDto);
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, StrUtil.toUnderlineCase(orderBy));
        List<TypeMapping> typeMappings = typeMappingDao.queryAll(typeMapping);
        return PageResult.builder()
                .total(page.getTotal())
                .rows(typeMappingMapper.toDto(typeMappings))
                .build();
    }

    @Override
    public void create(TypeMappingDto typeMappingDto) {
        TypeMapping typeMapping = typeMappingMapper.toEntity(typeMappingDto);
        int insert = typeMappingDao.insert(typeMapping);
        Assert.isTrue(insert > 0, "创建失败");
    }


    @Override
    public void update(TypeMappingDto typeMappingDto) {
        TypeMapping typeMapping = typeMappingMapper.toEntity(typeMappingDto);
        int update = this.typeMappingDao.updateById(typeMapping);
        Assert.isTrue(update > 0, "更新失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    @Override
    public void deleteByIds(Set<Long> ids) {
        for (Long id : ids) {
            typeMappingDao.deleteById(id);
        }
    }
}